package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lqq
 * @date 2020-01-15 15:07
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    /**
     * 新增权限
     * @param permission
     */
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 权限分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询,基于mybatis框架提供的分页助手
        PageHelper.startPage(currentPage,pageSize);
      Page<Permission> page=  permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();


        return new PageResult(total,rows);
    }

    public void add(Permission permission) {
        permissionDao.add(permission);

    }

    /**
     * 根据id删除权限
     * @param id
     */
    public void deleteById(Integer id) {
        //判断当前权限是否已经关联到角色
       long count= permissionDao.findCountByCheckItemId(id);
        System.out.println(count);
       if(count>0){
           //当前权限是否已经关联到角色,不许删除
          //抛个异常
           new RuntimeException();
       }
       permissionDao.deleteById(id);
    }

    /**
     * 根据id查权限信息,回显
     * @param id
     * @return
     */
    public Permission findById(Integer id) {
      Permission permission=  permissionDao.findById(id);
        return permission;
    }

    /**
     * 修改权限
     * @param permission
     */
    public void edit(Permission permission) {

        permissionDao.edit(permission);
    }

    /**
     * 查询所有权限
     * @return
     */
    public List<Permission> findAll() {

        return permissionDao.findAll();
    }
}
